/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */
package nl.dualit.clazzified.store.clazz.db.dao;

import java.util.Map;

import nl.dualit.clazzified.store.FieldTypes;
import nl.dualit.clazzified.store.clazz.AttributeFields;

/**
 * This class provides sql statements for operations on
 * the Attribute database table.
 * 
 * @author Steven Ottenhoff
 *
 */
class AttributeSql {
	
	static String insertAttribute( Map<AttributeFields,String> data ){
		
		StringBuilder sql = new StringBuilder();
		
		sql.append( "INSERT INTO " + AttributeFields.tableName() + " ( " );
		int i = 0;
		for( AttributeFields field : AttributeFields.values() ){
			if( field != AttributeFields.ATTRIBUTE_ID ){
				sql.append( ( ( i > 0 ) ? ", " : "" ) + field.columnName() );
				i = 1;
			}
		}
		sql.append( " ) VALUES ( " );
		i = 0;
		for( AttributeFields field : AttributeFields.values() ){
			if( field != AttributeFields.ATTRIBUTE_ID ){
				if( i > 0 )
					sql.append( "," );
				
				sql.append( ( ( field.columnType().equals( FieldTypes.STRING ) ? " '" : " ") ) );
				sql.append( ( ( data.get( field ) != null ) ? data.get( field ) : "" ) );				
				if( field.columnType().equals( FieldTypes.STRING ) )
					sql.append( "'" );
				i = 1;
			}			
		}
		sql.append( " )" );
		return sql.toString();
		
	}
	
	static String updateAttribute( Map<AttributeFields,String> data ){
		
		StringBuilder sql = new StringBuilder();
		
		sql.append("UPDATE " + AttributeFields.tableName() + " SET" );
		int i = 0;
		for( AttributeFields field : AttributeFields.values() ){
			if( field != AttributeFields.ATTRIBUTE_ID ){
				sql.append(  ( ( i > 0 ) ? ", " : " " ) );
				sql.append( field.columnName() + " = " );
				if( field.columnType().equals( FieldTypes.STRING  ) )
					sql.append( "'" );
				sql.append( ( ( data.containsKey( field ) ? data.get( field ) : "" ) ) );
				if( field.columnType().equals( FieldTypes.STRING  ) )
					sql.append( "'" );
				i = 1;
			}
		}
		sql.append( " WHERE " + AttributeFields.ATTRIBUTE_ID.columnName() + " = " );
		sql.append( data.get( AttributeFields.ATTRIBUTE_ID ) );
		
		return sql.toString();
	
	}
	
	static String selectAttributes( AttributeFields clause, String value ){
		
		StringBuilder sql = new StringBuilder();
		sql.append( "SELECT " );
		int i = 0;
		for( AttributeFields field : AttributeFields.values() ){
			sql.append( ( ( i > 0 ) ? ", " : "" ) + field.columnName() );
			i = 1;
		}
		sql.append( " FROM " + AttributeFields.tableName() );
		sql.append( " WHERE " + clause.columnName() );
		sql.append( " = " );
		sql.append( ( ( clause.columnType().equals( FieldTypes.STRING ) ) ? "'"  : "" ) );
		sql.append( value );
		sql.append( ( ( clause.columnType().equals( FieldTypes.STRING ) ) ? "'"  : "" ) );
		
		return sql.toString();
	
	}
	
	static String deleteAttributes( AttributeFields clause, String value ){
		
		StringBuilder sql = new StringBuilder();
		sql.append( "DELETE FROM " + AttributeFields.tableName() );
		sql.append( " WHERE " + clause.columnName() );
		sql.append( " = " );
		sql.append( ( ( clause.columnType().equals( FieldTypes.STRING ) ) ? "'"  : "" ) );
		sql.append( value );
		sql.append( ( ( clause.columnType().equals( FieldTypes.STRING ) ) ? "'"  : "" ) );
		
		return sql.toString();
	}

}
